import { defineStore } from "pinia";

/**
 * 
 * 组合式   API 
 *  count 改造
 * 
 */

import { ref, reactive, computed } from "vue";


// 参数 id, 匿名函数
const useCountStore = defineStore('count', () => {

    // 定义state 即数据
    const name = ref('frank')
    const age = ref(25)
    const gender = ref('男')
    let sum = ref(5)


    // 定义方法 
    function increment(value: number) {
        sum.value += value
    }


    // getters 要写
    const bigSum = computed(
        () => {
            return sum.value * 10
        }
    )

    // getters 定义
    const upperName = computed(() => {
        return name.value.toUpperCase()
    })

    return { name, age, gender, sum, increment, bigSum, upperName }

})



// 选项式API 
const useCountStore1 = defineStore('count', {
    state: () => {
        return {
            name: "frank",
            age: 25,
            gender: "男",
            sum: 5,
        }
    },

    //放置 的动作函数 
    actions: {
        increment(value: number) {
            this.sum += value
        }

    },

    // 可以定义getters 类似于计算属性 
    getters: {
        bigSum(state) {
            return state.sum * 10
        },

        upperName(state) {
            // console.log('aaaaa',this)
            return state.name.toUpperCase()
        }
    }
})


// 导出
export default useCountStore;